//
//  SinaHomeTableViewCell.m
//  oc微博
//
//  Created by 安陆与 on 16/1/22.
//  Copyright © 2016年 安陆与. All rights reserved.
//

#import "SinaHomeTableViewCell.h"
#import <UIKit/UIKit.h>
#import "Masonry.h"
#import "SinaOriginalView.h"
#import "SinaStatusViewModel.h"
#import "SinaStatusModel.h"
#import "SinaFooterView.h"
#import "SinaRetweetView.h"

// 只要添加了这个宏，就不用带mas_前缀
#define MAS_SHORTHAND
// 只要添加了这个宏，equalTo就等价于mas_equalTo
#define MAS_SHORTHAND_GLOBALS
//这个头文件一定要写在宏定义下面
#import "Masonry.h"

@interface SinaHomeTableViewCell()

@property (nonatomic, weak) SinaOriginalView *originalView;

@property (nonatomic, weak) SinaFooterView *footerView;

@property (nonatomic, weak) SinaRetweetView *retweetView;

@property (nonatomic,strong) MASConstraint* bottomConstraint;
@end

@implementation SinaHomeTableViewCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
    }
    
    return self;
}


-(void)setModel:(SinaStatusViewModel *)model{
    
    _model = model;

    _originalView.originalModel = model;
    
    _footerView.footerModel = model;
    
//    _retweetView.retweetModel = model;

    _retweetView.backgroundColor = [UIColor redColor];
    
    [_bottomConstraint uninstall];
    
    NSLog(@"??????%@",model.model.retweeted_status);
    
    if (model.model.retweeted_status) {
        
        [_footerView needsUpdateConstraints];
        
        [_footerView updateConstraintsIfNeeded];
        
        [_footerView mas_updateConstraints:^(MASConstraintMaker *make) {
           _bottomConstraint = make.top.equalTo(self.retweetView.mas_bottom);
        }];
        
        _retweetView.hidden = NO;
        
        //MARK:这里修改了
        
        _retweetView.retweetModel = model;

        
   
    }else{
    
        [_footerView needsUpdateConstraints];
        
        [_footerView updateConstraintsIfNeeded];
        
        [_footerView mas_updateConstraints:^(MASConstraintMaker *make) {
          _bottomConstraint =  make.top.equalTo(self.originalView.mas_bottom);
        }];
        
        _retweetView.hidden = YES;

    
    }

}


-(void)setupUI{

    SinaOriginalView *original = [[SinaOriginalView alloc]init];
    
    [self.contentView addSubview:original];
    
    self.originalView = original;
    
    //约束
    [original mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self.contentView.mas_left);
        make.top.equalTo(self.contentView.mas_top);
        make.width.equalTo(self.contentView.mas_width);
        
    }];
    
    SinaRetweetView *retweetView = [[SinaRetweetView alloc]init];
    
    self.retweetView = retweetView;
    
    [self.contentView addSubview:retweetView];
    
    SinaFooterView *footerView = [[SinaFooterView alloc]init];
    
    [self.contentView addSubview:footerView];
    
    self.footerView = footerView;
    

    
    
    [retweetView mas_makeConstraints:^(MASConstraintMaker *make) {
        
//        CGFloat kMargin = self.contentView.bounds.size.width - 20;
        
        make.top.equalTo(_originalView.mas_bottom);
        make.width.mas_equalTo(self.mas_width);
        make.left.equalTo(self.mas_left);
//        make.height.mas_equalTo(20);
//      make.bottom.equalTo(self.footerView.mas_top);
    }];
    
    [footerView mas_makeConstraints:^(MASConstraintMaker *make) {
//        _bottomConstraint =make.top.equalTo(self.originalView.mas_bottom);
        make.left.equalTo(self.contentView.mas_left);
        make.width.equalTo(self.contentView.mas_width);
        make.height.mas_equalTo(44);
        
        
    }];
    


    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.mas_top);
        make.left.equalTo(self.mas_left);
        make.right.equalTo(self.mas_right);
        make.bottom.equalTo(self.footerView.mas_bottom);
    }];
    
    

}

@end
